// 定义组合式函数, 将公共的逻辑代码抽离出来
import { ref, onMounted, onUnmounted } from "vue";

//按需导出
export function useMouse() {
    const x = ref(0);
    const y = ref(0);

    const updateFn = (event) => {
        // event 为事件对象
        x.value = event.pageX;
        y.value = event.pageY;
    };
    // 绑定事件
    onMounted(() => {
        window.addEventListener("mousemove", updateFn);
    });
    //移除事件
    onUnmounted(() => {
        window.removeEventListener("mousemove", updateFn);
    })
    return {
        x, y
    }
}
